package 力扣;

import java.util.*;

/**
 * @author yyq
 * @create 2022-04-06 15:49
 */
public class leetcode102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        int LayerNodes = 1;
        Queue<TreeNode> queue=new LinkedList<>();
        List<List<Integer>> lists = new ArrayList<>();
        if(root==null) return lists;
        queue.add(root);
        while (!queue.isEmpty()){
            int temp = 0;
            List<Integer> list=new ArrayList<>();
            while (LayerNodes!=0){
                TreeNode treeNode = queue.poll();
                list.add(treeNode.val);
                if(treeNode.left!=null)
                {
                    queue.add(treeNode.left);
                    temp++;
                }
                if(treeNode.right!=null)
                {
                    queue.add(treeNode.right);
                    temp++;
                }
                LayerNodes--;
            }
            lists.add(list);
            LayerNodes=temp;
        }
        return lists;
    }
}
